package com.tt.payee.utils.file;

import com.qiniu.common.QiniuException;
import com.qiniu.http.Response;
import com.qiniu.storage.UploadManager;
import com.qiniu.util.Auth;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.util.UUID;


/**
 * @author Tang
 * @Description: 七牛云文件上传工具类
 * @date:2020-04-10 14:48
 */
@Slf4j
@Controller
public class QiniuCloudUtil {

    /**
     * ACCESS_KEY
     */
    private final static String ACCESS_KEY = "eMi1ayo5A6K1fHqUOLof27DR2jSnMP0fyVuDH8dX";

    /**
     * SECRET_KEY
     */
    private final static String SECRET_KEY = "DY4YvBVRchdgFeHRW1CJRo8ufpbG2o8PqFiURX44";

    /**
     * 要上传的空间
     */
    private final static String BUCKETNAME = "image";


    /**
     * 密钥配置
     */
    private static Auth auth = Auth.create(ACCESS_KEY, SECRET_KEY);


    /**
     * 创建上传对象
     */
    private static UploadManager uploadManager = new UploadManager();

    /**
     * 上传单个七牛云文件（返回七牛云地址）
     *
     * @param file 文件
     * @return 文件名称和问文件地址
     */
    public String qiNiuuploadFileOne(MultipartFile file) {

        Response res;
        String path;

        try {

            String fileName = file.getOriginalFilename();

            assert fileName != null;
            String fileSuffix = fileName.substring(fileName.indexOf("."));
            log.info("文件名后缀" + fileSuffix);
            String key1 = UUID.randomUUID() + fileSuffix;

            //调用put方法上传
            log.info("开始调用上传方法");
            res = uploadManager.put(file.getBytes(), key1, auth.uploadToken(BUCKETNAME));
            path = res.jsonToMap().get("key").toString();

            log.info("文件名称。。。。。" + path);

            return path;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 上传单个七牛云文件（返回七牛云文件访问地址）
     *
     * @param file 文件 fileIsName 文件名称
     * @return 文件名称和问文件地址
     */
    public String qiNiuuploadFileOne(MultipartFile file, String fileIsName) {

        Response res;
        String path;

        try {
            String fileSuffix = fileIsName.substring(fileIsName.indexOf("."));
            log.info("文件名后缀" + fileSuffix);
            String key1 = UUID.randomUUID() + fileSuffix;

            //调用put方法上传
            log.info("开始调用上传方法");
            res = uploadManager.put(file.getBytes(), key1, auth.uploadToken(BUCKETNAME));
            path = res.jsonToMap().get("key").toString();

            log.info("文件名称。。。。。" + path);

            return path;
        } catch (IOException e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 上传单个图片返回图片id
     *
     * @param file
     * @return
     */
    public String uploadingfile(MultipartFile file) {

        Response res;
        String path;

        try {

            String fileSuffix = file.getOriginalFilename().substring(file.getOriginalFilename().indexOf("."));
            log.info("文件名后缀" + fileSuffix);
            String key1 = UUID.randomUUID() + fileSuffix;

            //调用put方法上传
            res = uploadManager.put(file.getBytes(), key1, auth.uploadToken(BUCKETNAME));
            path = res.jsonToMap().get("key").toString();


            return path;
        } catch (Exception e) {
            log.error("文件上传失败", e);

            return null;
        }
    }


    public static String getUploadToken() throws Exception{
        String upToken = auth.uploadToken(BUCKETNAME);
        return upToken;
    }

}
